package com.ophiux.cs.application.user.impl.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ophiux.cs.application.consultBase.dto.AddDoctorRequest;
import com.ophiux.cs.application.consultBase.dto.deptCreateVadilRequest;
import com.ophiux.cs.application.consultBase.dto.hospitalCreateVadilRequest;
import com.ophiux.cs.application.consultBase.impl.AddDoctorAppService;
import com.ophiux.cs.application.consultBase.impl.DeptGeneralService;
import com.ophiux.cs.application.consultBase.impl.HospitalGeneralService;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.application.core.Throws.ParamException;
import com.ophiux.cs.application.user.dto.addUserByDoctorRequest;
import com.ophiux.cs.application.user.dto.addUserByDoctorResponse;
import com.ophiux.cs.domain.consultBase.model.entity.Dept;
import com.ophiux.cs.domain.consultBase.model.entity.Hospital;
import com.ophiux.cs.domain.user.domainservice.UserDomainService;
import com.ophiux.cs.domain.user.model.entity.User;
import com.ophiux.cs.domain.user.model.valueobj.UserValueObj;
import com.ophiux.cs.domain.user.repositories.IUserRepository;
import com.ophiux.utils.common.HanyuPinyinHelper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class addUserByDoctorService {
	@Autowired
	IUserRepository iUserRepository;
	@Autowired
	UserDomainService userDomainService;
	@Autowired
	HospitalGeneralService hospitalGeneralService;// 医院信息服务相关
	@Autowired
	DeptGeneralService deptGeneralService;// 科室信息服务相关
	@Autowired
	AddDoctorAppService addDoctorAppService;// 新增医生信息服务

	@Transactional
	public Result<addUserByDoctorResponse> addUserByDoctor(addUserByDoctorRequest dto) {
		Result<addUserByDoctorResponse> result = new Result<addUserByDoctorResponse>();
		addUserByDoctorResponse res = new addUserByDoctorResponse();
		try {
			try {
				// 验证参数是否允许为空
				dto.verParam();
			} catch (ParamException e) {
				return Result.fail(ResultEnum.requestParamFail.getCode(), e.getMessage());
			}

			/**
			 * 根据电话号码和角色 判断是否已经注册
			 */
			UserValueObj obj = new UserValueObj();
			obj.setUSER_ACC(dto.getPHONE());
			obj.setRolePatient();

			User user = iUserRepository.getUser(obj);
			if (user == null) {
				/**
				 * 医院信息入库
				 */
				hospitalCreateVadilRequest hospCreateVDto = new hospitalCreateVadilRequest();
				hospCreateVDto.setHOSPITAL_NAME(dto.getHOSPITAL_NAME());
				Hospital hospitalInfo = hospitalGeneralService.hospitalCreateVadil(hospCreateVDto);

				/**
				 * 科室信息入库/更新
				 */
				deptCreateVadilRequest deptCreateVDto = new deptCreateVadilRequest();
				deptCreateVDto.setDEPT_NAME(dto.getDEPT_NAME());
				deptCreateVDto.setHOSPITAL_ID(hospitalInfo.getHOSPITAL_ID());
				Dept deptInfo = deptGeneralService.deptCreateVadil(deptCreateVDto);

				User userInfo = new User();
				userInfo.setUSER_ACC(dto.getPHONE());
				userInfo.setUSER_NAME(dto.getUSER_NAME());
				userInfo.setPHONE(dto.getPHONE());
				userInfo.setUSER_PWD(dto.getUSER_PWD());
				userInfo.setRoleDoctor();

				boolean saveResult = iUserRepository.savaUser(userInfo);
				if (saveResult) {// 保存成功
					/**
					 * 医生信息入库
					 */
					AddDoctorRequest addDoctorDto = new AddDoctorRequest();
					addDoctorDto.setHOSPITAL_ID(hospitalInfo.getHOSPITAL_ID());
					addDoctorDto.setDEPT_ID(deptInfo.getDEPT_ID());
					addDoctorDto.setDOCTOR_NAME(dto.getUSER_NAME());
					addDoctorDto.setTECHNICAL_TITLE(dto.getTECHNICAL_TITLE());
					addDoctorDto.setZY_CERTIFICATE_NO(dto.getCERTIFICATE_NO());
					addDoctorDto.setSEX(dto.getSEX());
					HanyuPinyinHelper pinYinhelper = new HanyuPinyinHelper();

					addDoctorDto.setPINYIN(pinYinhelper.toHanyuPinyin(dto.getUSER_NAME()));
					addDoctorDto.setPHONE(dto.getPHONE());
					addDoctorDto.setCERTIFICATE_TYPE("1");// 暂时默认为1 身份证
					addDoctorDto.setCERTIFICATE_NO(dto.getID_NO());

					addDoctorAppService.createDoctor(addDoctorDto);

					result = Result.success(res);

				} else {
					result = Result.fail(ResultEnum.registerFail.getCode(), ResultEnum.registerFail.getMsg());
				}
			}else {
				Result.fail(ResultEnum.userAgin.getCode(), ResultEnum.userAgin.getMsg());
			}

		} catch (Exception e) {
			log.info("addUserInfo接口异常：", e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}

		return result;

	}
}
